package com.fyt.ZhongYaoApp.mapper;

import com.fyt.ZhongYaoApp.pojo.Browsingrecord;
import com.fyt.ZhongYaoApp.pojo.Essay;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface EssayMapper {

    @Select("select * from essay where 1 = 1")
    List<Essay> EssayList();

    @Select("select * from essay where id = #{id}")
    Essay findEssayById(Integer id);

    @Insert("insert into browsingrecord(essay_id,user_id,access_time) values (#{essayId},#{userId},now())")
    void addBrowsingrecord(Browsingrecord browsingrecord);

    @Select("select e.*,bro.access_time from essay as e left join browsingrecord as bro on bro.essay_id = e.id where bro.user_id = #{userId} order by bro.access_time DESC")
    List<Essay> findAccessTimeByUserId(Integer userId);

    @Select("select * from browsingrecord where user_id = #{userId} and essay_id = #{essayId}")
    Browsingrecord findAccessTimeIfExisByUserId(Browsingrecord browsingrecord);

    @Update("update browsingrecord set access_time = now() where user_id = #{userId} and essay_id = #{essayId}")
    void updateAccessTime(Browsingrecord browsingrecord);
}
